package com.linewow.xhyy.nihaonew.activity.user;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.FrameActivity;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.base.BaseActivity;

import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LXR on 2016/10/25.
 */
public class LanguageActivity extends BaseActivity {
    @Bind(R.id.toolbar_find_title)
    TextView toolbarFindTitle;
    @Bind(R.id.toolbar_find_left)
    ImageView toolbarFindLeft;
    @Bind(R.id.toolbar_find_right)
    ImageView toolbarFindRight;
    @Bind(R.id.toolbar_find_right_tv)
    TextView toolbarFindRightTv;
    @Bind(R.id.toolbar_find_toolbar)
    Toolbar toolbarFindToolbar;
    @Bind(R.id.language_chinese)
    AppCompatRadioButton languageChinese;
    @Bind(R.id.language_english)
    AppCompatRadioButton languageEnglish;
    @Bind(R.id.language_radio)
    RadioGroup languageRadio;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_language);
        ButterKnife.bind(this);
        toolbarFindRight.setVisibility(View.GONE);
        toolbarFindRightTv.setVisibility(View.VISIBLE);
        toolbarFindRightTv.setText(getResources().getString(R.string.widget_sure));
        toolbarFindLeft.setImageResource(R.mipmap.icon_back_white);
        toolbarFindTitle.setText(getString(R.string.title_language));
        if(NiHaoApp.EnglishFlag){
            languageEnglish.setChecked(true);
        }else{
            languageChinese.setChecked(true);
        }
    }



    @OnClick({R.id.toolbar_find_left, R.id.toolbar_find_right_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_find_left:
                finish();
                break;
            case R.id.toolbar_find_right_tv:

                int id=languageRadio.getCheckedRadioButtonId();
                changeLanguage(id);
                break;
        }
    }

    private void changeLanguage(int id) {
        Resources resources = LanguageActivity.this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        switch (id){
            case R.id.language_english:
                config.locale= Locale.US;
                NiHaoApp.EnglishFlag=true;
                break;
            case R.id.language_chinese:
                config.locale=Locale.CHINA;
                NiHaoApp.EnglishFlag=false;
                break;
        }
        resources.updateConfiguration(config, dm);
//        Intent intent=new Intent(SecondActivity.this,MainActivity.class);
//        startActivity(intent);
        Intent intent = new Intent(this, FrameActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}
